// request.js
const app = getApp()
function request(options) {
  wx.request({
    url: options.url,
    method: options.method || 'GET',
    data: options.data,
    header: options.header,
    success: function (res) {
      if (res.statusCode === 401 || res.statusCode === 403) {
        // token 过期，跳转到登录页面
        wx.showToast({
          title: '登录已失效',
          icon: 'error'
        })
        app.removeGlobalUserInfo()
        app.removeGlobalToken()
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/login/login'
          });
        }, 1000)
      } else {
        options.success && options.success(res);
      }
    },
    fail: function (err) {
      options.fail && options.fail(err);
    },
    complete: function () {
      options.complete && options.complete();
    }
  });
}

module.exports = {
  request: request
};